﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;
using System.Web.Mvc;

namespace Lilium.Web.Mvc
{
	/// <summary>
	/// Base class for script resource references.
	/// </summary>
	/// <remarks>
	/// Thread Safety
	/// This type is thread safe.
	/// </remarks>
	public abstract class ScriptResourceReferenceBase: IScriptResourceReference
	{
		public override bool Equals(object obj)
		{
			return Equals(obj as IScriptResourceReference);
		}

		public override int GetHashCode()
		{
			return (LibraryName != null ? LibraryName.GetHashCode() : 0) ^ (ResourceName != null ? ResourceName.GetHashCode() : 0);
		}

		#region public string LibraryName

		private readonly string m_LibraryName;

		public string LibraryName { get { return m_LibraryName; } }

		#endregion

		#region public string ResourceName

		private readonly string m_ResourceName;

		public string ResourceName { get { return m_ResourceName; } }

		#endregion

		#region IEquatable<IScriptResourceReference> Members

		public bool Equals(IScriptResourceReference other)
		{
			return (other != null)
				&& (this.LibraryName == other.LibraryName)
				&& (this.ResourceName == other.ResourceName);
		}

		#endregion

		#region IScriptResourceReference Members

		public abstract void Render(AjaxHelper ajaxHelper);

		#endregion

		protected ScriptResourceReferenceBase(string libraryName, string resourceName)
		{
			if (string.IsNullOrEmpty(libraryName))
				throw new ArgumentNullOrEmptyException("libraryName");
			if (string.IsNullOrEmpty(resourceName))
				throw new ArgumentNullOrEmptyException("resourceName");

			m_LibraryName = libraryName;
			m_ResourceName = resourceName;
		}

		protected string GetResourceVirtualPath()
		{
			return  "/" + ScriptResourceController.ScriptResourceControllerName + "/" + LibraryName + "/" + ResourceName;
		}
	}
}